import Vue from 'vue';
import VueRouter from 'vue-router'
import App from '../App'
import list from './routermap';
import {getUser} from '../utils/searchHistory'
const routes=[{
    path:'/',
    component: App,
    children: list
}]
Vue.use(VueRouter);
const router = new VueRouter({
    routes,
    mode:'hash',
    // scrollBehavior(to, from, savedPosition) {
    //     // `to` 和 `from` 都是路由对象
    // }
    
})
router.beforeEach((to, from, next) => {
    let auth = to.matched.some(function (item) {
        return item.meta.auth
    })
    if (to.matched.length === 0) { 
        next('/404'); 
      } else if (auth) {
        let user = getUser();
        if(user && user.user_id){
            next();
        }else{
            next({
                path:'/login',
                query:{redirect:to.fullPath}
            })
        }
        
    } else 
        next();
})
  
export default router

